I am a fairly new developer with DXL and am trying to understand the SkipList object. I am looking through the Reference Manual and it discusses how to put items into a SkipList, but it doesn't show how to get a specific value if you have the key. In my DOORS module, I have 2 columns which give a list of text values to look for and values to replace that text with. And, yes, they are lined up such that the 2nd line in column 1 (ObjID) corresponds to the value in the 2nd line in column 2 (ObjText). What I'm doing is splitting each column into 2 arrays and placing the id/text pairs into a SkipList. Next, I would like to loop through the skiplist and search the requirement for each ID and replace the ID with the ObjText value from the skip list.
I have seen the find method which will determine if a key appears in the skiplist, but how do I get the value that the key corresponds to? The reason I wan't to use a skip list is because there may be multiple pairs of columns, but want to put all of these in a single skiplist. And I figure that performing the find function then retrieving the value would be easier than having to loop through an array each time you need to search for something. As an example, say I have 2 IDs named C_23 and C_27 and their corresponding values are 10 inches and 25 degrees respectively. I would like to put these in a skip list with C_23 and C_27 for keys and the other text items for values. Next, I have a requirement that use the keys, so I would search for "C_" in the requirement. If and when I find a "C_" I take the full key value and look for its correct text in the skip list. How would I retrieve the value associated with C_27 from a skip list named sl? Do I just use sl('C_27')?
Chris Christopher Cote - Tue Oct 25 12:11:33 EDT 2016 |
Re: Retrieving items from a SkipList I think your special form of searching can only be done with a for loop over the skip list with a regexp match for keys like "C_". If you use the for loop you have already the value. Only the match with the key will be done with a regexp (e.g. regexp "^C_[0-9]+"). |
Re: Retrieving items from a SkipList The find() function has a optional 3rd parameter, which is a out parameter returning the value. You can do the following:
Skip sl = createString();
put(sl, "C_1", 10);
put(sl, "C_21", 1041);
put(sl, "C_13", 103);
put(sl, "C_14", 1022);
put(sl, "C_15", 101);
put(sl, "C_16", 1110);
put(sl, "C_17", 106);
put(sl, "C_18", 104);
put(sl, "C_19", 103);
put(sl, "C_10", 102;
int value;
if(find(sl, "C_10", value)){
print value"\n";
}
Make sure to use createString() when creating a skiplist with string keys or you might run into issues later on. |
Re: Retrieving items from a SkipList O.Wilkop - Wed Oct 26 06:51:35 EDT 2016 The find() function has a optional 3rd parameter, which is a out parameter returning the value. You can do the following:
Skip sl = createString();
put(sl, "C_1", 10);
put(sl, "C_21", 1041);
put(sl, "C_13", 103);
put(sl, "C_14", 1022);
put(sl, "C_15", 101);
put(sl, "C_16", 1110);
put(sl, "C_17", 106);
put(sl, "C_18", 104);
put(sl, "C_19", 103);
put(sl, "C_10", 102;
int value;
if(find(sl, "C_10", value)){
print value"\n";
}
Make sure to use createString() when creating a skiplist with string keys or you might run into issues later on. Thank you O.Wilkop. I assume that if you did find(s1, "C23", value) that it would not print the value since it's not there which would give a false value for the find. Correct?
Chris |
Re: Retrieving items from a SkipList Christopher Cote - Wed Oct 26 07:50:08 EDT 2016 Thank you O.Wilkop. I assume that if you did find(s1, "C23", value) that it would not print the value since it's not there which would give a false value for the find. Correct?
Chris correct, find() returns true if the key was found and false if not. |
Re: Retrieving items from a SkipList O.Wilkop - Wed Oct 26 07:52:15 EDT 2016 correct, find() returns true if the key was found and false if not. OK, while I learned a lot from this post, I've just realized that because I am working within a template, I can't use DXL and its SkipList object. So now I need to change my original code into JavaScript.
var newTxt = ""
var OutText;
OutText = Object_Text; //Object_Text is an attribute in the DOORS database.
if (OutText.indexOf("C_")!=-1) {
arrConstIDs = ConstantList_ObjID.split(/\n/);
arrConstTxt = ConstantList_ObjTxt.split(/\n/);
if (arrConstIDs.length == arrConstTxt) { //Error check to make sure we have same number of items in each
for (var n=0; n<arrConstIDs.length; n++) {
put(skConst, arrConstIDs[n], arrConstTxt[n]);
}
}
var constantPre = "<C_";
var findStr = "/\b<C_[0-9]+>"; //<C_###>
while (OutText.indexOf(constantPre)!=-1) {
nPrefixLoc = OutText.indexOf(constantPre);
var sConstID = OutText.substr(nPrefixLoc, Object_Text.indexOf(">")+1);
if (find(skConst, sConstID, newTxt)) {
OutText.replace(sConstID, newTxt);
}
}
|